

// Claim Sets

function CreateClaimSet(clsnum,clsdate,phase,nbclaims,lab) {
  var date=new Date();
  //alert("create"+clsnum+" num claims "+ nbclaims +"  phase  " +phase);
  CLAIMSET = XMLDOMDOS.createElement("claim_set");
  
  // dossier number  in the procedure
  var attr = XMLDOMDOS.createAttribute("number");
  attr.value = clsnum;
  CLAIMSET.setAttributeNode(attr);
  
  // phase in the procedure
  attr = XMLDOMDOS.createAttribute("phase");
  attr.value = phase;
  CLAIMSET.setAttributeNode(attr);
  
  // claim set version
  attr = XMLDOMDOS.createAttribute("ver");
  attr.value = GetNextVersionNumber();
  CLAIMSET.setAttributeNode(attr);

  attr = XMLDOMDOS.createAttribute("rmks");
  attr.value = "";
  CLAIMSET.setAttributeNode(attr);

  // version date
  attr = XMLDOMDOS.createAttribute("ver_date");
  attr.value = clsdate;
  CLAIMSET.setAttributeNode(attr);

  // version label
  attr = XMLDOMDOS.createAttribute("lab");
  attr.value = lab;
  CLAIMSET.setAttributeNode(attr);
  
  // build claim array
  child = XMLDOMDOS.createElement("claims");
  for(var cl=1; cl <= nbclaims ; cl++) {
    var claim=CreateClaim(cl);
    child.appendChild(claim);    
  }
  CLAIMSET.appendChild(child);
  // Add single group
  child = XMLDOMDOS.createElement("groups");
  // v.90 Groups per doc
  attr = XMLDOMDOS.createAttribute("refdoc");
  child.setAttributeNode(attr);
  child.appendChild(CreateGroup());

 
  CLAIMSET.appendChild(child);
  return CLAIMSET;
}


function CloneClaimSet(tgtver,srcver) {
  var newcls=GetClaimSet(srcver).selectSingleNode("claims");
  var oldcls=GetClaimSet(tgtver).selectSingleNode("claims");
  if(newcls.childNodes.length!=oldcls.childNodes.length) {
    alert("Cannot clone sets of claims with different lenghts");
  } else {
    // replace claims
    GetClaimSet(tgtver).replaceChild(newcls.cloneNode(true),oldcls);
    // replace groups
    var newgrps=GetClaimSet(srcver).selectSingleNode("groups");
    var oldgrps=GetClaimSet(tgtver).selectSingleNode("groups");
    GetClaimSet(tgtver).replaceChild(newgrps.cloneNode(true),oldgrps);
    // select "new" claims 
    SelectClaimSet(tgtver);
    // update import info
    for(var cl=1; cl <= GetClaimSetLength(); cl++){      
      SetClaimImport(cl,srcver+"/"+cl);
    }
  }
}


// Claim Sets
function GetClaimSets() {
  return CLAIMSETS=DOSSIER.selectNodes("claim_set");
}


function GetClaimSetsLength() {
  return DOSSIER.selectNodes("claim_set").length;
}


//----------------------------------------------------------------------------------

function GetClaimSet(vers) {
  return (vers && CLAIMSETS[vers-1]) || CLAIMSET;
}

function GetClaimSetClaims(vers) {
  return GetClaimSet(vers).selectSingleNode("claims").childNodes;
}

function GetClaimSetGroups(vers) {
  return GetClaimSet(vers).selectSingleNode("groups").childNodes;
}

function AddClaimSet(clsnum,clsdate,phase,nbclaims,lab) {
  //alert("add  "+clsnum+" num claims "+ nbclaims+"  phase  " +phase);
  DOSSIER.appendChild(CreateClaimSet(clsnum,clsdate,phase,nbclaims,lab));  
}


//------------------------------------------------------------------------------

function SelectClaimSet(vers) {
  GetClaimSets();  
  CLAIMSET=GetClaimSet(vers); 
  if(CLAIMSET) {
    CLAIMS=GetClaimSetClaims();
    CLAIMGROUPS=GetClaimSetGroups();
    //CheckClaimSetIntegrity();
  } else if(vers!=1) {
    //alert("No claim set version "+ vers +" found. Opening claim set version 1 instead. ");
    SelectClaimSet(GetClaimSetsLength());
  } else {
    alert("No claim sets are available yet for this file");
    return 0;
  }
  InitFilters("List",GetPrefFilter(GetDossierNumber(),vers));
  return 1;
}

//------------------------------------------------------------------------------

function IncClaimsWarnings() {
  CLAIMSET.setAttribute("warnings",parseInt(CLAIMSET.getAttribute("warnings"))+1);
}

function DecClaimsWarnings() {
    CLAIMSET.setAttribute("warnings",parseInt(CLAIMSET.getAttribute("warnings"))-1);
}

function ResetClaimsWarnings() {
  CLAIMSET.setAttribute("warnings",0);
}

function GetClaimsWarnings() {
  return CLAIMSET.getAttribute("warnings");
}

//------------------------------------------------------------------------------


function CheckClaimIntegrity(cl) {
  SetClaimEqWarn(cl,"");
  SetClaimGroupWarn(cl,"");
  // rewrite to trimaran style
  SetClaimValue(cl,ClaimValueToClaimDocValue(GetClaimValue(cl)));
  var tf_iter= new claim_features_iterator(cl);
  while(!tf_iter.end()) {
    if(tf_iter.getEq()) {      
      eqtf= new claim_tf(tf_iter.getEq());     
      if(is_claim_tf(eqtf.getcl(),eqtf.gettf())) {
	if(GetFeatureEq(eqtf.getcl(),eqtf.gettf())) {
	  tf_iter.setWarn("Redirected tf equivalence: EQ("+cl+"."+tf_iter.gettf()+")="+
			  tf_iter.getEq()+" EQ("+tf_iter.getEq()+")="+GetFeatureEq(eqtf.getcl(),eqtf.gettf()));
	}
      } else {
	tf_iter.setWarn("Bad tf equivalence: EQ("+cl+"."+tf_iter.gettf()+")= non existant TF "+tf_iter.getEq());	
      }
    }    
    tf_iter.next();    
  }

  var eq=GetClaimEq(cl);
  if(eq) {
    if(is_claim(eq)) {
      if(GetClaimEq(eq)) {
	SetClaimEqWarn(cl,"Redirected claim equivalence: EQ("+cl+")="+eq+" but EQ("+eq+")="+GetClaimEq(eq));
      } 
      if(GetClaimGroup(cl) != GetClaimGroup(eq)) {
	SetClaimGroupWarn(cl,"EQ("+cl+")="+eq+" but  G("+cl+")="+(GetClaimGroup(cl)||"?")+" and G("+eq+")="+GetClaimGroup(eq));
      }
    } else {
      SetClaimEqWarn(cl,"Bad claim equivalence: EQ("+cl+")= non existant claim "+eq);
    }
  }

  // compute category
  if(!GetClaimCategory(cl)) {
    if(claim_isgrpindependent(cl)) {
      SetClaimCategory(cl,GetClaimDep(cl));
    } else {
      SetClaimCategory(cl,GetClaimDep(GetRootClaim(cl)));
    }    
  }
  // migrate old B to H
  if(is_invhint(GetClaimDoc_B(cl))) {
    SetClaimHint(cl,GetClaimDoc_B(cl));
    SetClaimDoc_B(cl,"");
  }


  set_Tainted(cl);
  updatefromdocs(cl);
  
  // put tf numbers 
  Renumberfeatures(cl);

}


  
function CheckClaimSetIntegrity() {
  //ResetClaimsWarnings();
  new_Taint();
  if(GetClaimSetPhase()=="FA") {
    SetClaimSetPhase("FR"); // fix old phase convention
  }
  for(var cl_iter= new claim_iterator(); !cl_iter.end(); cl_iter.next()) {
    if(cl_iter.getDep()) {
      CheckClaimIntegrity(cl_iter.getValue());
      for(var doc_iter= new doc_iterator();!doc_iter.end(); doc_iter.next()) {    
	/*if(GetClaimDocVal(cl_iter.getValue(),doc_iter.getPN())){
	  SetClaimDocUsrVal(cl_iter.getValue(),doc_iter.getPN(),
			 Strat_toVal(_GetClaimDocVal(cl_iter.getValue(),doc_iter.getPN())));
			 }*/
	UpdateClaimAutoVal(cl_iter.getValue(),doc_iter.getPN());
      }
      changeEq(cl_iter.getValue(),cl_iter.getEq());      
    }
  }
  //alert(GetClaimsWarnings()+" error(s) found in set");
  if(GetClaimsWarnings()>0) {
    alert(GetClaimsWarnings()+" error(s) found in set, fix them !");
  } 

}


//------------------------------------------------------------------------------      


function GetClaimSetVersion() {
  return (CLAIMSET && CLAIMSET.getAttribute("ver")) || "";
}

function GetClaimSetLabel(vers) {
  return GetClaimSet(vers).getAttribute("lab") || ("V"+(vers || GetClaimSetVersion()));
}

function SetClaimSetLabel(lab) {
  CLAIMSET.setAttribute("lab",lab);
}

function GetNextVersionNumber() {
  return GetClaimSetsLength()+1;
}


function GetClaimSetDate(vers) {
  return GetClaimSet(vers).getAttribute("ver_date");
}

function SetClaimSetDate(date) {
  return CLAIMSET.setAttribute("ver_date",date);
}

function GetClaimSetPhase(vers) {
  return GetClaimSet(vers).getAttribute("phase");
}

function GetClaimSetLaw(vers) {
  switch(GetClaimSet(vers).getAttribute("phase")) {
  case "EP":
    return "EPC";
  case "PCT":
    return "PCT";
  default:
    return "NATS"; // national offices 
  }
}

function SetClaimSetPhase(phase) {
  return CLAIMSET.setAttribute("phase",phase);
}

function GetClaimSetLength(vers) {
  return GetClaimSetClaims(vers).length;
}

// resize 
function SetClaimSetLength(newl) {
  var current=GetClaimSetLength();
  var root=CLAIMSET.selectSingleNode("claims");
  if(newl > current) {
    for(var cl=current+1; cl <= newl ; cl++) {
      root.appendChild(CreateClaim(cl));    
    }
  } else if(newl < current) {
    for(var cl=current; cl != newl ; cl--) {
      root.removeChild(CLAIMS[cl-1]);
    }    
  }
}

function ClaimSetAppNumNormalize(num) {
   return num.toUpperCase();
}

function GetClaimSetAppNum(vers) {
  return (GetClaimSet(vers) && GetClaimSet(vers).getAttribute("number").toUpperCase()) || "";
}

function SetClaimSetAppNum(app) {
  return CLAIMSET.setAttribute("number",ClaimSetAppNumNormalize(app));
}

function GetClaimSetRefDoc(vers) {
  return GetClaimSet(vers).getAttribute("refdoc");
}

function SetClaimSetRefDoc(doc) {
  CLAIMSET.setAttribute("refdoc",doc);
}

function GetClaimSetRmks(vers) {
  return GetClaimSet(vers).getAttribute("rmks") || "";
}

function SetClaimSetRmks(txt,vers) {
  //alert("Set attribute to "+txt);
  GetClaimSet(vers).setAttribute("rmks",txt);
}

// return version of the claims which first has phase==phase
function GetEntryPhase(phase) {
  for(var v=1; v <= GetClaimSetsLength(); v++) {
    if(GetClaimSetPhase(v)==phase) return v;
  }
  return 0;
}

function GetClaimSetLastDate() {  
  return GetClaimSetDate(GetClaimSetsLength());
}


function claimsets_iterator() {
  this._vers=1;
  this._last=GetClaimSetsLength();
}

claimsets_iterator.prototype.end = function() {
    return this._vers > this._last;
}

claimsets_iterator.prototype.next = function() {
    this._vers++;
}

claimsets_iterator.prototype.getValue = function() {
    return this._vers;
} 
  
claimsets_iterator.prototype.seek = function(vers) {
    this._vers=vers;
}

claimsets_iterator.prototype.reset = function(){
  this.seek(1);
}
  
claimsets_iterator.prototype.last = function() {
  return this._last;
}


claimsets_iterator.prototype.first = function() {
    return 1;
}

claimsets_iterator.prototype.getPri = function() {
    return GetDossierPri();
}


claimsets_iterator.prototype.getClaims = function() {
    var clist= new List();
    for(var n=1; n<= this.last(); n++) {
      clist.push(n);
    }
    return clist;
}

claimsets_iterator.prototype.getAppNum = function () {  return GetClaimSetAppNum(this._vers);}
claimsets_iterator.prototype.getDate = function ()   {    return GetClaimSetDate(this._vers);}
claimsets_iterator.prototype.getPhase = function ()  {   return GetClaimSetPhase(this._vers);}
claimsets_iterator.prototype.getRmks = function ()   {   return GetClaimSetRmks(this._vers);}
claimsets_iterator.prototype.getLength = function () {  return GetClaimSetLength(this._vers);}
claimsets_iterator.prototype.getLabel = function ()  {  return GetClaimSetLabel(this._vers); }

claimsets_iterator.prototype.select = function(vers) {
  if(vers) {
    this._vers=vers;
  }
  SelectClaimSet(this._vers);
}   
